package health.lm.com.home.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import health.lm.com.component_base.base.event.BusProvider;
import health.lm.com.component_base.base.event.IBus;
import health.lm.com.component_base.base.mvp.fragment.XLazyFragment;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.data.HttpCST;
import health.lm.com.home.R;
import health.lm.com.home.R2;
import health.lm.com.home.adapter.HomePageAdapter;
import health.lm.com.home.entity.HomePageEntity;
import health.lm.com.home.presenter.HomePagePresenter;
import health.lm.com.router.ARouterConstant;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.home.fragment
 * Date：2019/8/20 13:51
 * Introduce：首页一般图文资讯列表
 */
public class HomePageFragment extends XLazyFragment<HomePagePresenter> implements BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    //OnTouch 记录当前position的位置，用于判断是上滑还是下滑
    int preX1;
    int preY1;
    int preX2;
    int preY2;
    private HomePageAdapter mPageAdapter;
    private String parentId;//一级分类ID 用于判断 是否是视频栏目
    private String id;//二级分类ID
    private int page = 0;
    private int pageSize = 10;
    private boolean isScrollTop = false;

    public static HomePageFragment getInstance(String id,String parentId) {
        HomePageFragment pageFragment = new HomePageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        bundle.putString("parentId", parentId);
        pageFragment.setArguments(bundle);
        return pageFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_page;
    }

    @Override
    public HomePagePresenter newP() {
        return new HomePagePresenter();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void initData(Bundle savedInstanceState) {
        id = Objects.requireNonNull(getArguments()).getString("id");
        parentId = Objects.requireNonNull(getArguments()).getString("parentId");
        //mPageAdapter = new HomePageAdapter(new ArrayList<>());
        //mPageAdapter.setOnLoadMoreListener(this, mRvList);
        //mPageAdapter.setEnableLoadMore(true);
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        //mRvList.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)));
        initDataAdapter();
        //mRvList.setAdapter(mPageAdapter);
        //mRvList.setNestedScrollingEnabled(false);
        getP().getData(id, page, pageSize);

        mRvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //判断 RecyclerView是否滚动到顶部
                isScrollTop = !mRvList.canScrollVertically(-1);
            }
        });

        //触摸判断是否为上滑，
        mRvList.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    preX1 = (int) event.getX();
                    preY1 = (int) event.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    preX2 = (int) event.getX();
                    preY2 = (int) event.getY();
                    //判断是否为下拉
                    if (preX1 < preX2 && preY1 < preY2 && isScrollTop) {
                        sendEnvet();
                    }
                    break;
            }
            return false;
        });
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getP().getData(id, page, pageSize);
    }

    private void initDataAdapter() {
        //AMenuId 2 视频新闻，其他 图文新闻
        if ("2".equals(parentId)) mPageAdapter = new HomePageAdapter(new ArrayList<>(), 2);
        else mPageAdapter = new HomePageAdapter(new ArrayList<>());
        mPageAdapter.setOnLoadMoreListener(this, mRvList);
        mPageAdapter.setEnableLoadMore(true);
        mRvList.setAdapter(mPageAdapter);

        mPageAdapter.setOnItemClickListener((adapter, view, position) -> {
            HomePageEntity pageEntity = (HomePageEntity) adapter.getData().get(position);
            //点击跳转新闻详情
            ARouter.getInstance().build(ARouterConstant.JSWebActivity)
                    .withString("url", pageEntity.getJump_url())
                    .withString(HttpCST.SHARE_URL, pageEntity.getJump_url())
                    .navigation();
        });
    }

    public void setNewData(List<HomePageEntity> pNewData) {
        isScrollTop = true;//默认规定滚动到顶部
        mPageAdapter.addData(pNewData);
        if (pNewData.size() <= pageSize) mPageAdapter.loadMoreEnd();
        else {
            mPageAdapter.loadMoreComplete();
        }
        if (mPageAdapter.getData().size() <= 0) mPageAdapter.setEmptyView(empty());
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        if (null != mPageAdapter) mRvList.setAdapter(mPageAdapter);
    }

    /**
     * 发送滚动事件
     */
    private void sendEnvet() {
        BusProvider.getBus().post(new IBus.AbsEvent() {
            @Override
            public int getTag() {
                return 1000;
            }
        });
    }
}
